<?php
namespace app\commands;

use yii;
use yii\console\Controller;
use app\lib\BaseFunction;

class ServerController extends Controller
{
    public $server;
    
    public function actionStart()
    {
        $this->server = new \swoole_websocket_server("0.0.0.0", 9501);
        
        $this->server->on('open', function (\swoole_websocket_server $server, $request) {
                echo "server: handshake success with fd{$request->fd}\n";
                var_dump($request->get['token']);
        });
        $this->server->on('message', function (\swoole_websocket_server $server, $frame) {
                echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
                $server->push($frame->fd, "this is server");
        });
        $this->server->on('close', function ($ser, $fd) {
                echo "client {$fd} closed\n";
        });

        $this->server->start();
    }
}
